<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Controllers\CheckController;
use Session;
use DB;


class UserController extends Controller
{
    /**
     * [用户登录界面]
     * @return {[page] [用户登录界面]
     */
    public function getLogin(Request $req)
    {
      //在登陆页面进行判断用户是否已经登陆,如果已经登陆,跳转到首页，没登陆才可注册
      return Session::get('user_nick') ? redirect('index') :
      view('home.login');
    }



    /**
    * [post用户注册信息]
    * @return {[bool] [用户注册成功返回true,注册失败返回false]
    */
    public function postReg(Request $req)
    {

      //******************判断注册类型[是否是第三方登录]***********************
      //开启事务
      DB::beginTransaction();

      /*开始事务体,如果有错,抛出异常*/
      try {

          //获取req传过来的注册信息--users表数据
          $user_salt = substr(md5(mt_rand(10000,99999)),0,6);
          $users = [
              'user_name'     => $req->input('user_name'),
              'user_nick'     => $req->input('user_nick'),
              'user_phone'    => $req->input('user_phone'),
              'user_salt'     =>  $user_salt,
              'user_password' => md5($user_salt . $req->input('user_password')),
              'regtime'       => time(),
              'last_log'      => time(),
          ];
          //开始插入users表
          $uid = DB::table('users')->insertGetId($users);

          //关联user_exts和user_games表,初始化
          DB::table('user_exts')->insert([
              'uid'       => $uid,
              'user_phone'=>$users['user_phone'],
          ]);
          DB::table('user_games')->insert(['uid' => $uid]);

          //提交事务
          DB::commit();

          //写入session
          $req->session()->put([
              'user_id'   => $uid,
              'user_name' => $users['user_name'],
              'user_nick' => $users['user_nick'],
              'user_status' => 1,
	 ]);
      } catch(\Illuminate\Database\QueryException $ex) {

          //抛出异常后回滚事务
          DB::rollback();

          //返回错误信息
          return view('notice',[
              'status' => 'Error',
              'msg'    => '用户注册失败,请重新注册',
              'url'    => url('/user/reg'),
              'time'   => '3',
          ]);
      }

      //成功跳转到首页,自动登陆上
      return redirect('/index');
    }

    /**
    * [获取注册页面]
    * @return {[page] [注册页面]
    */
    public function getReg(Request $req)
    {
      /*判断用户是否已经登陆,如果已经登陆,跳转到提示页面,以后全用弹窗代替*/
      return Session::get('user_nick') ? redirect('/index') :
      view('home/reg');
    }
    /**
     * [post用户登录的信息]
     * @return {[bool] [登陆是否成功]
     */
    public function postCheckpwd(Request $req)
    {
        //  if ($req->veryfy !== Session::get('captcha')) {
        //    return view('/notice',[
        //         'status' => 'Error',
        //         'msg'    => '用户不存在,请重新登录',
        //         'url'    => url('user/login'),
        //         'time'   => 3,
        //     ]);
        // }

        $type = $req->input('type2');
        $pass = $req->input('user_password');
        $res = DB::table('users')
                ->select('user_id','user_name','user_nick','user_password','user_salt','user_status')
                ->where($type,$req->input('user_name'))
                ->first();


        if ($res === null || $res->user_status==0) {
            return view('notice',[
                'status' => 'Error',
                'msg'    => '用户不存在,请重新登录',
                'url'    => url('user/login'),
                'time'   => 3,
            ]);
        }

         if ($res->user_status == 3) {
            return view('/notice',[
                'status' => 'Error',
                'msg'    => '用户已封号，如有疑问，联系客服',
                'url'    => url('/user/login'),
                'time'   => 3,
            ]);
        }

        if (Session::get('captcha')===$req->input('veryfy')) {
            return view('notice',[
                'status' => 'Error',
                'msg'    => '验证码错误,请重新输入',
                'url'    => url('user/login'),
                'time'   => 3,
            ]);
        }

        if ($res->user_password !== md5($res->user_salt.$pass)) {
            return view('/notice',[
                'status' => 'Error',
                'msg'    => '用户密码错误,请重新登录',
                'url'    => url('user/login'),
                'time'   => 3,
            ]);
        }

        if ($res->user_password === md5($res->user_salt.$pass)) {
            $req->session()->put([
                'user_id'   => $res->user_id,
                'user_name' => $res->user_name,
                'user_nick' => $res->user_nick,
                'user_status' => $res->user_status,
            ]);
            return redirect('/index');
        }
    }

    /**
     * [用于弹窗的登录验证]
     * @param  Request $req [表单的输入值]
     * @return [page]       [原页面]
     */
    public function postTancheckpwd(Request $req)
    {
        $type = $req->input('type2');
        $pass = $req->input('user_password');
        $res = DB::table('users')->select('user_id','user_name','user_nick','user_password','user_salt','user_status')->where($type,$req->input('user_name'))->first();
        if ($res === null) {
            return view('/notice',[
                'status' => 'Error',
                'msg'    => '用户不存在,请重新登录',
                'url'    => url('/user/login'),
                'time'   => 3,
            ]);
        }

        if (Session::get('captcha')===$req->input('veryfy')) {
            return view('/notice',[
                'status' => 'Error',
                'msg'    => '验证码错误,请重新输入',
                'url'    => url('/user/login'),
                'time'   => 3,
            ]);
        }

        if ($res->user_password !== md5($res->user_salt.$pass)) {
            return view('/notice',[
                'status' => 'Error',
                'msg'    => '用户密码错误,请重新登录',
                'url'    => url('/user/login'),
                'time'   => 3,
            ]);
        }

        if ($res->user_password === md5($res->user_salt.$pass)) {
            $req->session()->put([
                'user_id'   => $res->user_id,
                'user_name' => $res->user_name,
                'user_nick' => $res->user_nick,
            ]);
            return back();
        }
    }
    /**
     * [用户推出]
     * @return {[redirect] [销毁session,返回首页]
     */
    public function getQuit(Request $req)
    {
      $bool = $req->session()->flush();
      return  view('/notice',[
          'status' => 'Success',
          'msg'    => '用户退出成功',
          'url'    => url('/index'),
          'time'   => '3',
      ]);
    }

    /**
     * [用户中心的首页]*
     * @return {[page] [个人中心页面]
     */
    public function getUsercente()
    {
         //判断用户是否已经登陆,如果未登陆,跳转登录页
        if(!(Session::get('user_nick'))){
           return redirect('/user/login');
        } else{
            return view('usercenter.index');
        }
    }
    /**
     * [获取修改密码页面]*
     * @return {[page] [修改密码页面]
     */
    public function getEditpwd()
    {

      if (Session::get('up_token') != md5('true')) {
          return view('/notice',[
              'status' => 'Error',
              'msg'    => '非法操作',
              'url'    => url('/index'),
              'time'   => '3',
          ]);
      }
      return view('home.editpwd');
    }

    /**
     * [获取post提交信息]
     * @return {[bool] [修改成功返回true,修改失败返回false]
     */
    public function postEditpwd(Request $req)
    {

      $user_id = Session::get('user_id');
      $user_salt = substr(md5(mt_rand(10000,99999)),0,6);
      $pwd = md5($user_salt . $req->user_password);
      $res = DB::table('users')
                    ->where('user_id',$user_id)
                    ->update(['user_password'=>$pwd,'user_salt'=>$user_salt]);
      $msg = $res ? '修改成功' : '修改失败';

      return view('/notice',[
          'status' => 'Error',
          'msg'    => $msg,
          'url'    => url('/user/login'),
          'time'   => '3',
      ]);
    }

    /**
     * [获取修改密码昵称页面]*
     * @return {[page] [修改昵称页面]
     */
    public function getEditnick()
    {
        //判断用户是否已经登陆,如果未登陆,跳转登录页
        if(!(Session::get('user_nick'))){
           return redirect('/user/login');
        } else{
            return view('usercenter/accset/upnic');
        }

    }

    /**
     * [获取post提交昵称修改信息]*
     * @return {[bool] [修改成功返回true,修改失败返回false]
     */
    public function postEditnick(Request $req)
    {
        //判断用户是否已经登陆,如果未登陆,跳转登录页
        if(!(Session::get('user_nick'))){
           return redirect('/user/login');
        } else{
            // //再次正则验证
            // $matres = $this->checkMatch("user_nick",$req->user_nick);
            // if(!$matres){
            //     return "0"; //正则验证不通过,返回0
            // }
            //获得用户昵称
            $uid = Session::get('user_id');
            //进行昵称的修改
            $result = DB::table('users')->where('user_id',$uid)->update(['user_nick'=>$req->user_nick]);
            //修改session中存储的昵称
             if ($result) {
                Session::put('user_nick',$req->user_nick);
             }
            //返回修改结果
            return $result?"昵称修改成功":"昵称修改失败";
        }

    }

    /**
     * [获取修改绑定手机页面]*
     * @return {[page] [修改绑定手机页面]
     */
    public function getEditphone()
    {
        //判断用户登录
        if(!(Session::get('user_id'))){
            return redirect('/user/login');
        }else{
            //跳转到修改绑定手机页面
            //查询用户的手机号,用于在前台页面展示
            $phone = DB::table('users')->select('user_phone')->where('user_id',Session::get('user_id'))->first();
            //存储手机号
            $oldphone = $phone->user_phone;
            //隐藏手机号中间部分
            $phone = substr_replace($phone->user_phone, '****', 3, 4);
            Session::put(['phone'=>$phone]);//将带*号的手机号存起来,方便后续使用
            return view('usercenter/accset/upmobile',['phone'=>$phone,'oldphone'=>$oldphone]);
        }

    }

    /**
     * [获取post提交手机修改信息]
     * @return {[bool] [修改成功返回true,修改失败返回false]
     */
    public function postEditphone(Request $req)
    {
      //判断是否登录
      if (!Session::get('user_id')) {
        return redirect('user/login');
      } else {

          //验证新的手机号
          if ( $req->new_yzm != Session::get('phonecode')) {
            return "验证码输入错误";
          }
          //查询是否重复
          $over = DB::table('users')->select('user_phone')->where('user_id',Session::get('user_id'))->first();
          if ($req->new_phone == $over->user_phone) {
            return "您输入的手机号码已被注册";
          }
          //修改电话号码
          $row = ['user_phone'=>$req->new_phone];
          $result = DB::table('users')->where('user_id',Session::get('user_id'))->update($row);//users表
          $grade = DB::table('user_exts')->where('uid',Session::get('user_id'))->update($row);//usr_exts表
          //返回修改结果
          $bool = $result && $grade;
          return $bool ? "手机号修改成功" : "手机号修改失败";

      }

    }

    /**
     * [验证旧的手机号]
     * @return [page] [新手机号的表单]
     */
    public function postOldphone(Request $req)
    {
      //判断是否登录
      if (!Session::get('user_id')) {
        return redirect('user/login');
      } else {

          //验证输入的旧手机号验证码
          if ( $req->old_yzm != Session::get('phonecode')) {
            return "验证码输入错误";
          }
          //展示新手机号输入页
          return view('usercenter/accset/upmobile1');

      }

    }

    /**
     * [获取修改绑定邮箱页面]
     * @return {[page] [修改绑定邮箱页面]
     */
    public function getEditemail()
    {
        //判断用户是否登录
        if (!Session::get('user_id')) {
            return redirect('user/login');
        }else{
            //查询邮箱在前台页面显示
            $email = DB::table('user_exts')->select('user_email')->where('uid',Session::get('user_id'))->first();

            //将邮箱中的部分换成*
            $beemail = strstr($email->user_email, '@');
            $email = substr_replace($email->user_email,'****',2).$beemail;
            Session::put(['oldemail'=>$email]);
            return view('usercenter/accset/upemail',['oldemail'=>$email]);
        }

    }
    /**
     * [旧邮箱的验证,新邮箱页的展示]
     * @param  Request $req [description]
     * @return [type]       [description]
     */
    public function postOldemail(Request $req)
    {

      //验证登录
      if (!Session::get('user_id')) {
        return redirect('user/login');
      } else {
        //验证用户输入的邮箱是否存在
        $oemail = DB::table('user_exts')->select('user_email')->where('uid', Session::get('user_id'))->first();
        if ($oemail->user_email != $req->old_email) {
          return '请确认您输入的邮箱是否正确';
        }
        //验证用户输入的验证码
        if ($req->rev_oyzm == Session::get('rand')){
          //输入正确,把新的邮箱填写界面发给用户
          return view('usercenter/accset/upemailnew');
      } else {
          return '您密码输错了';
      }

        }

    }

    /**
     * [获取post提交绑定邮箱信息]
     * @return {[bool] [修改成功返回true,修改失败返回false]
     */
    public function postEditemail(Request $req)
    {
      //验证登录
      if(!Session::get('user_id')){
        return redirect('user/login');
      } else {
        //验证用户输入的验证码是否正确
        if ($req->rev_nyzm == Session::get('rand')) {
          //填写正确开始修改邮箱
          $result = DB::table('user_exts')->where('uid', Session::get('user_id'))->update(['user_email' => $req->new_email]);
          return $result ? '邮箱修改成功' : '由于网络原因,邮箱未能修改成功';
        } else {
          return '验证码填写错误';
        }

      }
      
    }

    /**
     * [获取修改密保页面]
     * @return {[page] [修改密保页面]
     */
    public function getEditmibao()
    {
        if (!Session::get('user_id')) {
            return redirect('user/login');
        }else{
            //查询密保问题
            $question = DB::table("user_exts")->select('user_question','user_answer')->where('uid',Session::get('user_id'))->first();
            //把查出的问题和答案存进session
            Session::put('old_question', $question->user_question);
            Session::put('old_answer', $question->user_answer);

            return view('usercenter/accset/upsecurity',['question'=>$question->user_question]);
        }

    }

    /**
     * [获取post提交修改密保信息]
     * @return {[bool] [修改成功返回true,修改失败返回false]
     */
    public function postEditmibao(Request $req)
    {
        //判断登录状态
        if (!Session::get('user_id')) {
            return rdirect('user/login');
        }else{
            // dd(Session::get('old_answer'));
            //验证旧的密保问题
            // dd($req->old_answer);
            $old_result = ($req->old_answer == Session::get('old_answer') );
            // dd($old_result);
            if(!$old_result){
                return "问题回答错误";
            }
            //存储新的密保问题
            $row = [
            'user_question' => $req->new_question,
            'user_answer' => $req->new_answer,
            ];
            //保存新的密保问题和答案
            $new_result = DB::table('user_exts')->where('uid', Session::get('user_id'))->update($row);
            //返回修改结果
            return $new_result ? "密保修改成功" : "您的网络不稳定,先玩会斗地主再改吧";

        }

    }

    /**
     * [比赛详情展示页]
     * @return [page] [比赛详情页]
     */
    public function getGamedetail(Request $req)
    {
        if (!Session::get('user_id')) {
            return redirect('user/login');
        }else{
            return view('usercenter/detail/gadetl');
        }

    }

    /**
     * [比赛详情页按需求进行展示]*
     * @param  Request $req [查询需求]
     * @return [page]  gadetl模板     [查询结果展示]
     */
    public function postGamedetail(Request $req)
    {
        if (!Session::get('user_id')) {
            return redirect('user/login');
        }else{
            if ($req) {

                $result = DB::table('user_games')
                  ->select('user_ddzwin','user_ddzlose','user_esywin','user_esylose','user_xqwin','user_xqlose','user_wzqwin','user_wzqlose','user_djscount','user_djshigh','user_cbkcount','user_cbkhigh')
                  ->where('uid',Session::get('user_id'))
                  ->get(); //查询比赛数据
                $result = $result[0]; //外面数组只有一个元素,取出,方便代码书写
                // dd($result);
                 //判断查询类型
                  switch ($req->gametype) { //用户要查询的数据
                    case 1 :  //斗地主
                      $gameinfo = [
                        'total' => $result->user_ddzwin + $result->user_ddzlose, //总场数
                        'win' => $result->user_ddzwin / ($result->user_ddzwin + $result->user_ddzlose) * 100, //胜率
                        'lose' => $result->user_ddzlose / ($result->user_ddzwin + $result->user_ddzlose) * 100, //负率
                        'type' => '斗地主',
                        ];
                      break;
                    case 2 : //21点
                      $gameinfo = [
                        'total' => $result->user_esywin + $result->user_esylose,
                        'win' => $result->user_esywin / ($result->user_esywin + $result->user_esylose) * 100,
                        'lose' => $result->user_esylose / ($result->user_esywin + $result->user_esylose) * 100,
                        'type' => '21点',
                        ];
                       break;
                    case 3 : //象棋
                      $gameinfo = [
                        'total' => $result->user_xqwin + $result->user_xqlose,
                        'win' =>$result->user_xqwin / ($result->user_xqwin + $result->user_xqlose) * 100,
                        'lose' =>$result->user_xqwin / ($result->user_xqwin + $result->user_xqlose) * 100,
                        'type' => '象棋',
                        ];
                      break;
                    case 4 : //五子棋
                      $gameinfo = [
                        'total' => $result->user_wzqwin + $result->user_wzqlose,
                        'win' => $result->user_wzqwin / ($result->user_wzqwin + $result->user_wzqlose) * 100,
                        'lose' =>$result->user_wzqlose / ($result->user_wzqwin + $result->user_wzqlose) * 100,
                        'type' => '五子棋',
                        ];
                      break;
                    case 5 : //打僵尸
                      $gameinfo = [
                        'total' => $result->user_djscount,
                        'high' => $result->user_djshigh,
                        'type' => '打僵尸',
                        ];
                      break;
                    case 6 : //踩白块
                      $gameinfo = [
                        'total' => $result->user_cbkcount,
                        'high' => $result->user_cbkcount,
                        'type' => '踩白块',
                        ];
                      break;
                  }

              return view('usercenter/detail/gadetl',['gameinfo'=>$gameinfo]);
                }
            }

    }

     /**
     * [获取所有消息页面]*
     * @return {[page] [所有消息页面]
     */
    public function getAllmessage(Request $req)
    {

        //DB类查询messages表
        //后期增加写进缓存,暂时省略
        $allmess = DB::table('messages')
            ->select('id', 'mess_content', 'mess_sendtime', 'mess_status')
            ->paginate(5);
        //将查询信息放到模板进行展示
      return view('usercenter.message.message', ['allmess'=> $allmess]);

    }

    /**
     * [获取未读邮件]
     * @return {[page] [未读消息页面]
     */
    public function getNotread()
    {
    
            //数据库查询未读信息
            $notread = DB::table('messages')
                ->select('id','mess_content','mess_sendtime', 'mess_status')
                ->where('mess_status',0)
                ->paginate(5);
                return view('usercenter/message/notread',['notread'=> $notread]);
      

    }

    /**
     * [获取已读邮件]
     * @return {[page] [已读消息页面]
     */
    public function getRead()
    {
            //数据库查询已读信息
            $read = DB::table('messages')
                ->select('id','mess_content','mess_sendtime','mess_status')
                ->where('mess_status',1)
                ->paginate(5);
                return view('usercenter/message/overead',['read'=> $read]);
        

    }


    /**
     * [我的煤球展示页面]
     * @return [page] [展示煤球页面]
     */
    public function getMymoney()
    {
        if (!Session::get('user_id')) {
          return redirect('user/login');
        }else{
          //查询用户的煤球总数
          $meiqiu = DB::table('user_games')->select('user_meiqiu')->where('uid',Session::get('user_id'))->first();
          return view('usercenter.money.mymoney', ['meiqiu'=>$meiqiu]);
        }

    }

    /**
     * [充值记录展示页]
     * @return [type] [description]
     */
    public function getMonrec()
    {
        return view('usercenter/money/moneyrec');
    }

    /**
     * [充值中心的展示页面]
     * @return [type] [description]
     */
    public function getChrmoney()
    {
        //先判断用户是否登录
         if(!(Session::get('uid'))){
           return redirect('/user/login');
         } else{
            return view('usercenter.money.chrmoney');
         }
    }



/********************** --start 忘记密码 -- ************************/
    public function getForgot()
    {
        return view('home.forgot');
    }

    public function postForgot(Request $req)
    {
        //判断用户传过来的密保类型及其值
        $nameor = $req->user_name;
        $usertype = preg_match('/^1[34578]{1}\d{9}$/', $req->user_name) ? 'user_phone' : 'user_name';
        $type = 'user_phone';               //选择找回密码的方式[可选]
        //创建返回消息数组

        //查询出要找回密码的用户id
        $res = DB::table('users')
                        ->select('user_id')
                        ->where($usertype,$nameor)
                        ->first();
        //判断查询结果，如果查询结果为空，返回报错页面
        if (!$res) {
            $mess = [
                'status' => 'Error',
                'url'    => '/user/forgot',
                'time'   => '3',
                'msg' => '用户名或手机号不存在'
            ];
            return view('/notice',$mess);
        }

        //查询出用户注册时的手机
        $user_phone = DB::table('users')->select($type)->where('user_id',$res->user_id)->first();

        //如果注册时的手机和新填写不一致，返回许错误页面
        if ($user_phone->user_phone != $req->phone) {
            $mess = [
                'status' => 'Error',
                'url'    => '/user/forgot',
                'time'   => '3',
                'msg' => '手机与注册时填写不一致'
            ];
            return view('/notice',$mess);
        }

        //如果验证码也生成的验证码填写不一致，返回报错
        if (Session::get('phonecode') != $req->veryfy) {
            $mess = [
                'status' => 'Error',
                'url'    => '/user/forgot',
                'time'   => '3',
                'msg' => '验证码输入错误'
            ];
            return view('/notice',$mess);
        }

        Session::put('up_token',md5('true'));
        Session::put('user_id',$res->user_id );
        return redirect('user/editpwd');
    }
/********************** --end 忘记密码 -- ************************/

/********************** --start 用户认证 -- ************************/

    public function getRenzheng()
    {
        $user_phone = DB::table('users')->select('user_phone')->where('user_id',Session::get('user_id'))->first();
        return view('home.renzheng',['user_phone'=>$user_phone]);
    }


    public function postRenzheng(Request $req)
    {
        $exts = [
            'user_email' => $req->user_email,
            'user_phone' => $req->user_phone,
            'user_question' => $req->user_question,
            'user_answer' => $req->user_answer,
            'user_truename'=> $req->user_truename,
            'user_idcard' => $req->user_idcard,
        ];
        $user = [
            'user_phone' => $req->user_phone,
            'user_status' => 2,
        ];
        //手机验证码
        if ($req->phoneyzm == Session::get('phonecode')) {
          $user_exts = DB::table('user_exts')->where('uid',Session::get('user_id'))->update($exts);
          $users = DB::table('users')->where('user_id',Session::get('user_id'))->update($user);
          Session::put('user_status',2);
        if ($user_exts && $users) {
            $mess = [
                  'status' => 'Success',
                  'url'    => '/',
                  'time'   => '3',
                  'msg'    => '认证成功'
              ];
              return view('/notice',$mess);
          }
        }else{
          $mess = [
                'status' => 'Success',
                'url'    => '/user/renzheng',
                'time'   => '3',
                'msg'    => '手机验证码错误,请重新输入'
            ];
            return view('/notice',$mess);
        }


    }

/********************** --end 用户认证 -- ************************/

/*********************** --start 微博登陆 -- *********************/
    public function getWeibo()
    {
        return \Socialite::with('weibo')->redirect();
    }

    public function getCallback(Request $req)
    {
        $oauthUser = \Socialite::with('weibo')->user();
        $user_openid = $oauthUser->getId();             //微博用户的id
        //判断该用户在数据库里已经存在
        $res = DB::table('users')
            ->select('user_id','user_name','user_nick','user_status')
            ->where('user_openid',$user_openid)
            ->first();

        if (!$res) {
            //如果查询结果为空,说明是第一次登陆,
            DB::beginTransaction();

            /*开始事务体,如果有错,抛出异常*/
            try {
                //获取req传过来的注册信息--users表数据
                $nick = '煤球用户' . mt_rand(100000,999999);
                $users = [
                    'user_openid' => $user_openid,
                    'user_nick' => $nick,
                    'regtime'     => time(),
                    'last_log'    => time(),
                ];
                //开始插入users表
                $uid = DB::table('users')->insertGetId($users);

                //关联user_exts和user_games表,初始化
                DB::table('user_exts')->insert([
                    'uid'       => $uid,
                ]);

                DB::table('user_games')->insert([
                    'uid' => $uid,
                    'user_pic' => $oauthUser->getAvatar(),
                ]);

                //提交事务
                DB::commit();

                //写入session
                $req->session()->put([
                    'user_id'   => $uid,
                    'user_nick' => $nick,
                    'user_openid' => $user_openid,
                    'user_status' => 1,
		]);
                //跳转到完善页面
                $mess = [
                      'status' => '友情提示',
                      'url'    => '/index',
                      'time'   => '3',
                      'msg'    => '微博登陆成功'
                ];
                return view('/notice',$mess);

            } catch(\Illuminate\Database\QueryException $ex) {

                //抛出异常后回滚事务
                DB::rollback();

                //返回错误信息
                return view('notice',[
                    'status' => 'Error',
                    'msg'    => '用户微博注册失败,请重新注册',
                    'url'    => url('/user/reg'),
                    'time'   => '3',
                ]);
            }
        }

        //如果微博用户已存在,调用登陆方法
        Session::put([
            'user_id' => $res->user_id,
            'user_name' => $res->user_name,
            'user_nick' => $res->user_nick,
            'user_status' => $res->user_status,
        ]);
	dd($req->session()->all());
        return redirect('/index');
    }
/************************ --end 微博登陆 -- **********************/

}
